package com.leo;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebHistory;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

/**
 * @ClassName WebViewDemo
 * @Description
 * @Author wangss
 * @date 2020.07.01 00:24
 * @Version 1.0
 */
public class WebViewDemo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        AnchorPane an = new AnchorPane();

        WebView webView = new WebView();
        WebEngine engine = webView.getEngine();
        engine.load("https://www.baidu.com");

//        webView.setFontScale(0.5);

        webView.setZoom(0.8);

        // 禁用右键菜单功能
//        webView.setContextMenuEnabled(false);

        // 这里可以加载html
//        engine.loadContent("");

        an.getChildren().addAll(webView);

        Scene scene = new Scene(an);
        primaryStage.setScene(scene);
        primaryStage.setHeight(800d);
        primaryStage.setWidth(1000d);
        primaryStage.setAlwaysOnTop(true);
        primaryStage.show();

        webView.prefWidthProperty().bind(an.widthProperty());
        webView.prefHeightProperty().bind(an.heightProperty());

        // 解决关窗口bug
        primaryStage.setOnCloseRequest(event -> {
            engine.load("");
            Platform.exit();
        });
    }
}
